package com.ysj.demo.accompanynetwork.mvp.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.ysj.demo.accompanynetwork.activity.RegisterActivity;
import com.ysj.demo.accompanynetwork.bean.BaseBean;
import com.ysj.demo.accompanynetwork.bean.RegisterBean;
import com.ysj.demo.accompanynetwork.bean.RestRsp;
import com.ysj.demo.accompanynetwork.bean.VerificationBean;
import com.ysj.demo.accompanynetwork.mvp.common.OnCommonListener;
import com.ysj.demo.accompanynetwork.mvp.model.RegisterModel;
import com.ysj.demo.accompanynetwork.mvp.view.RegisterView;

/**
 * Created by 80530 on 2018/8/18.
 */

public class RegisterPresenter {

    private RegisterModel mRegisterModel;

    private RegisterView mRegisterView;

    public RegisterPresenter() {
        mRegisterModel = new RegisterModel();
    }

    public void bind(RegisterActivity registerView) {
        mRegisterView = registerView;
    }


    /**
     * 注册业务
     */
    public void register() {
        String account = mRegisterView.getNick();
        String password = mRegisterView.getPassword();
        String phone = mRegisterView.getPhone();
        String code = mRegisterView.getCode();
        VerificationBean codeData = mRegisterView.getCodeBody();

        Log.e("TAG", "account:" + account + ",password" + password);
        if (checkParameter(account, password, phone, code, codeData)) {
            doSomePrepare();
            //登录 ，需要处理数据，所有要在model中执行
            RegisterBean bean = new RegisterBean();
            bean.setUsername(account);
            bean.setPassword(password);
            bean.setPhone(phone);
            bean.setVerifycode(code);
            bean.setValidatecode(codeData.getCokeKey());
            mRegisterModel.register(bean, new OnCommonListener() {
                @Override
                public void onSuccessListener(Object data) {
                    BaseBean info = (BaseBean) data;
                    mRegisterView.OnRegisterSuccess(info);
                }

                @Override
                public void onFailListener(Throwable t) {

                }
            });
        }
    }

    /**
     * 获取验证码业务
     */
    public void getVerifyCode() {
        mRegisterModel.getVerificationCode(new OnCommonListener() {
            @Override
            public void onSuccessListener(Object data) {
                RestRsp<VerificationBean> info = (RestRsp<VerificationBean>) data;
                if (info.getCode() == 0) {
                    mRegisterView.setCodeBody(info.getData());
                } else {
                    mRegisterView.setCodeBody(null);
                }
            }

            @Override
            public void onFailListener(Throwable t ){
                mRegisterView.setCodeBody(null);
            }
        });

    }

    /**
     * 做一些准备
     */
    private void doSomePrepare() {
        //这里可以设置按钮不可点击！否则一直点击登录也是不好
    }

    /**
     * 检测参数是否是否为空~~~
     *
     * @param account
     * @param password
     * @return
     */
    private boolean checkParameter(String account, String password, String phone, String code, VerificationBean codeData) {
        if (TextUtils.isEmpty(account)) {
            mRegisterView.showVerifyFailed("昵称不能为空");//提示错误
            return false;
        }

        if (TextUtils.isEmpty(password)) {
            mRegisterView.showVerifyFailed("密码不能为空");//提示错误
            return false;
        }

        if (TextUtils.isEmpty(phone)) {
            mRegisterView.showVerifyFailed("手机号不能为空");//提示错误
            return false;
        }

        if (TextUtils.isEmpty(code)) {
            mRegisterView.showVerifyFailed("验证码不能为空");//提示错误
            return false;
        }

        if (null == codeData) {
            mRegisterView.showVerifyFailed("验证码获取失败");//提示错误
            return false;
        }
        return true;
    }

}
